<template>
  <view class="content">
    <view class="navbar">
      <view
        v-for="(item, index) in navList"
        :key="index"
        class="nav-item"
        :class="{ current: tabCurrentIndex === index }"
        @click="tabClick(index)"
        >{{ item.text }}</view
      >
    </view>
    <swiper
      :current="tabCurrentIndex"
      class="swiper-box"
      duration="300"
      @change="changeTab"
    >
      <swiper-item
        class="tab-content"
        v-for="(tabItem, tabIndex) in navList"
        :key="tabIndex"
      >
        <scroll-view
          class="list-scroll-content"
          scroll-y
          @scrolltolower="loadData(tabIndex)"
        >
          <!-- 空白页 -->
          <u-empty
            text="暂无订单"
            mode="list"
            v-if="tabItem.loaded === true && tabItem.orderList.length === 0"
          ></u-empty>
          <!-- 订单列表 -->
          <view
            class="seller-view"
            :key="oderIndex"
            v-for="(order, oderIndex) in tabItem.orderList"
          >
            <!-- 店铺名称 -->
            <view class="seller-info u-flex u-row-between">
              <view class="seller-name wes" @click="navigateToStore(order)">
                <img src="" alt="" class="img" />
                <view class="name">用户昵称</view>
              </view>
              <view
                class="order-sn"
                :style="{
                  color:
                    order.orderStatus == 'UNDELIVERED' ||
                    order.orderStatus == 'UNPAID' ||
                    order.orderStatus == 'PARTS_DELIVERED' ||
                    order.orderStatus == 'TAKE' ||
                    order.orderStatus == 'STAY_PICKED_UP'
                      ? '#FF4252'
                      : '#757575',
                }"
                >{{ order.orderStatus | orderStatusList }}</view
              >
            </view>
            <view>
              <view>
                <view
                  class="goods-item-view"
                  @click="navigateToOrderDetail(order.sn)"
                  v-for="(goods, goodsIndex) in order.orderItems"
                  :key="goodsIndex"
                >
                  <view class="goods-img">
                    <u-image
                      border-radius="6"
                      width="100%"
                      height="100%"
                      :src="goods.image"
                    ></u-image>
                  </view>
                  <view class="goods-info-box">
                    <view class="goods-info">
                      <view class="goods-title wes"> 商品名称展示区域</view>
                      <view class="goods-price">
                        ￥{{ goods.goodsPrice | unitPrice }}
                      </view>
                    </view>

                    <view class="goods-info">
                      <view class="goods-specs wes">
                        描述描述描述描述描述描述描述描述描述描述</view
                      >
                      <view class="goods-num">
                        <view>x{{ goods.num }}</view>
                      </view>
                    </view>
                  </view>
                </view>
              </view>
              <view class="btn-view">
                <view class="order-description">
                  <div>订单金额</div>
                  <div class="price">￥{{ order.flowPrice | unitPrice }}</div>
                </view>
                <view class="dis-description">
                  <div>分佣金额</div>
                  <div class="price">￥{{ order.flowPrice | unitPrice }}</div>
                </view>
              </view>
            </view>
          </view>
          <uni-load-more :status="tabItem.loadStatus"></uni-load-more>
        </scroll-view>
      </swiper-item>
    </swiper>
  </view>
</template>

<script>
import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue";
import { getOrderList, cancelOrder, confirmReceipt } from "@/api/order.js";
import { getClearReason } from "@/api/after-sale.js";
import LiLiWXPay from "@/js_sdk/lili-pay/wx-pay.js";
export default {
  components: {
    uniLoadMore,
  },
  data() {
    return {
      lightColor: this.$lightColor,
      tabCurrentIndex: 0, //导航栏索引
      navList: [
        //导航栏list
        {
          state: 0,
          text: "全部",
          loadStatus: "more",
          orderList: [],
          pageNumber: 1,
        },
        {
          state: 2,
          text: "待发货",
          loadStatus: "more",
          orderList: [],
          pageNumber: 1,
        },
        {
          state: 3,
          text: "待收货",
          loadStatus: "more",
          orderList: [],
          pageNumber: 1,
        },
        {
          state: 4,
          text: "已完成",
          loadStatus: "more",
          orderList: [],
          pageNumber: 1,
        },
      ],
      status: "", //接收导航栏状态
      params: {
        pageNumber: 1,
        pageSize: 10,
        tag: "ALL",
      },
      orderStatus: [
        //订单状态
        {
          orderStatus: "ALL", //全部
        },
        {
          orderStatus: "WAIT_PAY", //代付款
        },
        {
          orderStatus: "WAIT_SHIP",
        },
        {
          orderStatus: "WAIT_ROG", //待收货
        },
        {
          orderStatus: "COMPLETE", //已完成
        },
        {
          orderStatus: "CANCELLED", //已取消
        },
        {
          orderStatus: "STAY_PICKED_UP", //待自提
        },
      ],
      cancelShow: false, //是否显示取消
      orderSn: "", //ordersn
      reason: "", //取消原因
      cancelList: "", //取消列表
      rogShow: false, //显示是否收货
    };
  },
  onShow() {
    if (this.$options.filters.tipsToLogin()) {
      if (!this.tabCurrentIndex) {
        this.initData(0);
      }
    }
    // this.loadData(this.status);
  },

  onLoad(options) {
    /**
     * 修复app端点击除全部订单外的按钮进入时不加载数据的问题
     * 替换onLoad下代码即可
     */
    let status = Number(options.status);
    this.status = status;

    this.tabCurrentIndex = status;
    // if (status == 0) {
    //   this.loadData(status);
    // }
  },

  watch: {
    /**监听更改请求数据 */
    tabCurrentIndex(val) {
      this.params.tag = this.orderStatus[val].orderStatus;
      //切换标签页将所有的页数都重置为1
      this.navList.forEach((res) => {
        res.pageNumber = 1;
        res.loadStatus = "more";
        res.orderList = [];
      });
      this.loadData(val);
    },
  },
  methods: {
    // 店铺详情
    navigateToStore(val) {
      uni.navigateTo({
        url: "/pages/product/shopPage?id=" + val.storeId,
      });
    },

    /**
     * 初始化数据
     */
    initData(index) {
      this.navList[index].pageNumber = 1;
      this.navList[index].loadStatus = "more";
      this.navList[index].orderList = [];
      this.loadData(index);
    },

    /**
     * 获取订单列表
     */
    loadData(index) {
      this.params.pageNumber = this.navList[index].pageNumber;
      // this.params.tag = this.orderStatus[index].orderStatus;
      getOrderList(this.params).then((res) => {
        uni.stopPullDownRefresh();
        if (!res.data.success) {
          this.navList[index].loadStatus = "noMore";
          return false;
        }
        let orderList = res.data.result.records;
        if (orderList.length == 0) {
          this.navList[index].loadStatus = "noMore";
        } else if (orderList.length < 10) {
          this.navList[index].loadStatus = "noMore";
        }
        if (orderList.length > 0) {
          this.navList[index].orderList =
            this.navList[index].orderList.concat(orderList);
          this.navList[index].pageNumber += 1;
        }
      });
    },
    //swiper 切换监听
    changeTab(e) {
      this.tabCurrentIndex = e.target.current;
    },
    //顶部tab点击
    tabClick(index) {
      this.tabCurrentIndex = index;
    },

    /**
     * 跳转到订单详情
     */
    navigateToOrderDetail(sn) {
      uni.navigateTo({
        url: "./orderDetail?sn=" + sn,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../uni.scss";
page,
.content {
  background: $page-color-base;
  height: 100%;
}

.swiper-box {
  height: calc(100vh - 40px);
  // #ifdef H5
  height: calc(100vh - 40px - 44px);
  // #endif
  padding: 16rpx;
}

.list-scroll-content {
  height: 100%;
}

.navbar {
  display: flex;
  height: 40px;
  padding: 0 5px;
  background: #fff;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
  position: relative;
  z-index: 10;

  .nav-item {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    font-size: 26rpx;
    color: #757575;
    position: relative;

    &.current {
      color: #212121;

      &:after {
        content: "";
        position: absolute;
        left: 50%;
        bottom: 0;
        transform: translateX(-50%);
        width: 44px;
        height: 0;
        border-bottom: 2px solid #212121;
      }
    }
  }
}

.seller-view {
  border-radius: 16rpx;
  background-color: #fff;
  margin-bottom: 16rpx;
  padding: 28rpx 24rpx 24rpx 24rpx;

  .seller-info {
    margin-bottom: 32rpx;
    .seller-name {
      flex: 10;
      font-size: 28rpx;
      font-weight: 600;
      display: flex;
      flex-direction: row;
      text-align: center;
      color: #212121;
      .img {
        margin-right: 16rpx;
        width: 48rpx;
        height: 48rpx;
        border-radius: 50%;
      }
    }

    .order-sn {
      font-size: 26rpx;
    }
  }
  .goods-info-box {
    flex: 1;
  }

  .goods-item-view {
    display: flex;
    // flex-wrap: wrap;
    flex-direction: row;

    .goods-img {
      width: 180rpx;
      height: 180rpx;
      margin-right: 24rpx;
      margin-bottom: 16rpx;
      border-radius: 16rpx;
      overflow: hidden;
    }

    .goods-info {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 16rpx;

      .goods-specs,
      .goods-num {
        font-size: 26rpx;
        color: #757575;
      }

      .goods-price,
      .goods-title {
        font-size: 28rpx;
        color: #212121;
      }
      .goods-specs,
      .goods-title {
        max-width: 360rpx;
      }
    }
  }

  .btn-view {
    font-size: 28rpx;
    margin-top: 18rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .order-description,
    .dis-description {
      color: #212121;
      display: flex;
    }
    .dis-description {
      color: #ff4252;
    }
  }
}
</style>